//
//  BaseModel.m
//  BabyStory
//
//  Created by 周文松 on 14-11-18.
//  Copyright (c) 2014年 com.talkweb.BabyStory. All rights reserved.
//

#import "BaseModel.h"
#import "ZWSRequest.h"

@implementation BaseModel

//+ (void )createData:(NSString *)responseString success:(void (^)(id data))success failure:(void (^)(NSString *msg, NSString *state))failure;
//{
//    NSLog(@"%@",responseString);
//    
//    NSData *data=[responseString dataUsingEncoding:NSUTF8StringEncoding];
//    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//    
//    BOOL isSuccess = [dict[@"success"] boolValue];
//    
//    NSString *msg = dict[@"msg"];
//    
//    if (isSuccess )
//    {
//        success(dict);
//    }
//    else
//    {
//        failure(msg,@"0");
//    }
//}

+ (void)createData:(NSString *)responseString success:(void (^)(id data))success failure:(void (^)(NSString *msg, NSString *state))failure {
    NSLog(@"%@", responseString);
    NSLog(@"成功");
    NSData *data=[responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSString *code = dict[@"code"];
    NSString * jsessionid = dict[@"jsessionid"];
    [keychainItemManager writeSessionId:jsessionid];
    NSString *msg = dict[@"note"];
    if ([code integerValue] == Status_Code_Request_Success) {
        success(dict);
    } else {
        failure(msg, code);
    }
}

+ (NSURLConnection *)POST:(NSString *)string parameter:(id)parameter class:( id)class  success:(void (^)(id data))success failure:(void (^)(NSString *msg, NSString *status ))failure {
    return  [ZWSRequest POST:string parameter:parameter success:^(NSString *responseString) {
                 [class createData:responseString success:^(id data) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                          success(data);
                      });
                  } failure:^(NSString *msg, NSString *status) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                          failure(msg,status);
                      });
                  }];
             } failure:^( NSString* msg, NSString *status) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     failure(msg,status);
                 });
             }];
}


@end
